#include "lj_log.h"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>

unsigned int lj_log_level = LJ_LL_V_DEBUG | LJ_LL_V_INFO | LJ_LL_V_WARN | LJ_LL_V_ERROR | LJ_LL_V_FATAL|LJ_LL_V_SET1|LJ_LL_V_SET2|LJ_LL_V_SET3;
unsigned int lj_log_head=1;

unsigned int time_sys_up=0;
unsigned int lj_now_ms(void)
{
    time_sys_up++;
    return time_sys_up;
}


void lj_set_log_head(unsigned char onoff)
{
    lj_log_head=onoff;
}

void lj_set_log_level(lj_log_level_t log_level)
{
    unsigned int value = 0;

    switch (log_level)
    {
    case LJ_LL_NONE:
        value |= LJ_LL_V_NONE;
        break;
    case LJ_LL_DEBUG:
        value |= LJ_LL_V_DEBUG;
    case LJ_LL_INFO:
        value |= LJ_LL_V_INFO;
    case LJ_LL_WARN:
        value |= LJ_LL_V_WARN;
    case LJ_LL_ERROR:
        value |= LJ_LL_V_ERROR;
    case LJ_LL_FATAL:
        value |= LJ_LL_V_FATAL;
        break;
    case LJ_LL_SET1:
        if(lj_log_level&LJ_LL_V_SET1)
        {
            value&=(!LJ_LL_V_SET1);
        }
        else{
            value|=LJ_LL_V_SET1;
        }
        break;
    case LJ_LL_SET2:
        if(lj_log_level&LJ_LL_V_SET2)
        {
            value&=(!LJ_LL_V_SET2);
        }
        else{
            value|=LJ_LL_V_SET2;
        }
        break;
    case LJ_LL_SET3:
        if(lj_log_level&LJ_LL_V_SET3)
        {
            value&=(!LJ_LL_V_SET3);
        }
        else{
            value|=LJ_LL_V_SET3;
        }
        break;
    default:
        break;
    }

    lj_log_level = value;
}



// lj_mutex_t log_mutex;

#ifndef lj_csp_printf
__attribute__((weak)) int lj_csp_printf(const char *fmt, ...)
{
    va_list args;
    int ret;

#ifdef LJ_DISABLE_ALL_LOGS
    if (0 == log_get_enable_lj_log_flag()) return;
#endif

    // ret = lj_mutex_lock(&log_mutex, LJ_WAIT_FOREVER);
    if (ret == 0)
    {
        va_start(args, fmt);
        ret = vprintf(fmt, args);
        va_end(args);

        fflush(stdout);
    }

    // lj_mutex_unlock(&log_mutex);

    return ret;
}
#endif //lj_csp_printf